package cn.lena.idle;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import cn.lena.idle.dao.UserDbHelper;
import cn.lena.idle.pojo.DTO.LoginUser;
import cn.lena.idle.pojo.DTO.Version;

/**
 * @author lena
 * @date 2021/6/13
 */
public class LoginActivity extends AppCompatActivity{

    private static final String TAG = "login";
    private Button loginBtn;
    private EditText useridEt;
    private EditText passEt;

    private TextView forgetPass;
    private TextView toRegist;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        // 单击登录按钮
        loginBtn = (Button) findViewById(R.id.loginBtn);
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickLogin();
            }
        });

        // 单击注册按钮
        toRegist = (TextView)findViewById(R.id.toRegist);
        toRegist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(LoginActivity.this,RegistActivity.class);
                startActivity(intent);
            }
        });

    }


    /**
     * 登录 t
     */
    public void onClickLogin() {
        useridEt = (EditText) findViewById(R.id.userId);
        passEt = (EditText) findViewById(R.id.pass);
        String userid = useridEt.getText().toString().trim();
        String pass = passEt.getText().toString().trim();
        if (userid.equals("")) {
            Toast.makeText(this, R.string.userIdError, Toast.LENGTH_LONG).show();
            return;
        }
        if (pass.equals("")) {
            Toast.makeText(this, R.string.passError, Toast.LENGTH_LONG).show();
            return;
        }

        UserDbHelper user_db=new UserDbHelper(getApplicationContext(),UserDbHelper.DB_NAME,null, Version.USER);
        String username=user_db.login(userid,pass);
        //System.out.println(username);
        if( !user_db.login(userid,pass).equals("") ){
            Toast.makeText(this, "欢迎您，"+username, Toast.LENGTH_LONG).show();
            // 页面跳转
            Intent intent_hello = new Intent(this, IndexActivity.class);
            startActivity(intent_hello);
            LoginActivity.this.finish();
        }else {
            Toast.makeText(this, R.string.loginError, Toast.LENGTH_LONG).show();
        }

    }
}